/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.sysmgmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.SystemConstant;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysRoleHpcEntity;
import com.cn.xiaonuo.tcrfinance.sysmgmt.enums.SystemEnum;
import com.cn.xiaonuo.tcrfinance.sysmgmt.mapper.SysRoleHpcMapper;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysRoleHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysRoleReParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysRoleMenuHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysRoleHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysUserRoleHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysUserHpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 角色
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class SysRoleHpcServiceImpl extends ServiceImpl<SysRoleHpcMapper, SysRoleHpcEntity> implements SysRoleHpcService {
	@Autowired
	private SysRoleMenuHpcService sysRoleMenuService;
	@Autowired
	private SysUserHpcService sysUserService;
    @Autowired
    private SysUserRoleHpcService sysUserRoleService;

	@Override
	public PageResult<SysRoleHpcEntity> queryPage(SysRoleReParam sysRoleParam) {

		QueryWrapper<SysRoleHpcEntity> queryWrapper = new QueryWrapper<>();
		if (ObjectUtil.isNotNull(sysRoleParam)) {

			// 根据唯一标识 查询
			if (ObjectUtil.isNotEmpty(sysRoleParam.getRoleName())) {
				queryWrapper.lambda().like(SysRoleHpcEntity::getRoleName, sysRoleParam.getRoleName());
			}

			// 根据商品唯一标识符 模糊查询
			if (ObjectUtil.isNotEmpty(sysRoleParam.getCreateUserId())) {
				queryWrapper.lambda().like(SysRoleHpcEntity::getCreateUserId, sysRoleParam.getCreateUserId());
			}
		}
		return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleHpcParam roleParam  ) {
		SysRoleHpcEntity role=new SysRoleHpcEntity();
		BeanUtil.copyProperties(roleParam,role);
        role.setCreateTime(new Date());
        this.save(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleHpcParam roleParam) {
		SysRoleHpcEntity role=new SysRoleHpcEntity();
		BeanUtil.copyProperties(roleParam,role);
        this.updateById(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


    @Override
	public List<Long> queryRoleIdList(Long createUserId) {
		return baseMapper.queryRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(SysRoleHpcEntity role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
		if(role.getCreateUserId() == SystemConstant.SUPER_ADMIN){
			return ;
		}

		//查询用户所拥有的菜单列表
		List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());

		//判断是否越权
		if(!menuIdList.containsAll(role.getMenuIdList())){
			throw new ServiceException(SystemEnum.ROLE_RANGE);
		}
	}
}
